<?php

namespace app\common\model\wash;

use app\common\model\factory\Category;
use think\Model;


class Goods extends Model
{

    

    

    // 表名
    protected $name = 'wash_goods';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text',
        'image_str',
        'category_name',
    ];
    

    public function getImageStrAttr($value, $data)
    {
        return $data['image'] ? cdnurl($data['image'], true) : '';
    }

    public function getCategoryNameAttr($value, $data)
    {
        if(isset($data['category_id']) && $data['category_id']){
            return Category::where('id',$data['category_id'])->value('title')??'';
        }
        return '';
    }


    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function cate()
    {
        return $this->belongsTo('app\common\model\factory\Category', 'category_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function admin()
    {
        return $this->belongsTo('app\common\model\Admin', 'company_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
